#include "Grid.h"
#include "glm/ext.hpp"
Grid::Grid() {
	GLShader gridVShader = GLShader("../Assets/Shaders/grid.vert");
	GLShader gridFShader = GLShader("../Assets/Shaders/grid.frag");
	gridProgram = new GLProgram(gridVShader, gridFShader);
	gridBufferSize = sizeof(PerFrameData);
}

Grid::~Grid() {
	delete gridProgram;
	glDeleteBuffers(1, &gridPerFrameDataBuffer);
	glDeleteVertexArrays(1, &vaoGrid);
	glDisable(GL_BLEND);
}

void Grid::Draw(const PerFrameData& perframe) {
	glNamedBufferSubData(gridPerFrameDataBuffer, 0, gridBufferSize, &perframe);
	gridProgram->useProgram();
	glDrawArraysInstancedBaseInstance(GL_TRIANGLES, 0, 6, 1, 0);
}

void Grid::Init() {
	//grid
	glCreateBuffers(1, &gridPerFrameDataBuffer);
	glNamedBufferStorage(gridPerFrameDataBuffer, gridBufferSize, nullptr, GL_DYNAMIC_STORAGE_BIT);
	glBindBufferRange(GL_UNIFORM_BUFFER, 0, gridPerFrameDataBuffer, 0, gridBufferSize);

	glCreateVertexArrays(1, &vaoGrid);
	glBindVertexArray(vaoGrid);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
